﻿using System;
using System.IO;
using System.Xml;

namespace SPSource.Lists
{
	public class ListManager
	{
        private int _listTemplateTypeCounter;//NOTE: needs to be unique for scope of feature

	    private ListTemplateManifestGenerator listTemplateManifestGenerator;
                
        private SPSourceFileInfo _spSourceFileInfo;

        public ListManager(SPSourceFileInfo spSourceFileInfo)
        {
            _spSourceFileInfo = spSourceFileInfo;
            _listTemplateTypeCounter = 10000;
            listTemplateManifestGenerator = new ListTemplateManifestGenerator();
        }

        public void GetList(XmlNode listTemplate)
		{
            try
            {
                int listTemplateType = _listTemplateTypeCounter;

                string listName = listTemplate.Attributes["Name"].Value;
                //TODO: only check if override not set!
                bool valid = false;
                if (!_spSourceFileInfo.OverrideFeatureDirectoryCheck)
                {
                    if (!FeatureManager.IsValidListSPSourceFileLocation(_spSourceFileInfo.SPSourceFileDirectoryInfo))
                    {
                        Utility.WriteError(String.Format("'{0}' file for ListTemplate must be only one folder deep in Feature folder (List {1})", _spSourceFileInfo.SPSourceFileName, listName));
                    }
                    else
                        valid = true;
                }
                else
                    valid = true;

                if (valid)
                {
                    if (!ListUtility.ListExists(listName, _spSourceFileInfo.DesignSiteUrl))
                    {
                        Utility.WriteError(String.Format("List titled '{0}' does not existing in design site!", listName));
                    }
                    else
                    {

                        Utility.WriteMessage("Reverse engineering list template: " + listName);

                        FileInfo schemaFile = ListTemplateSchemaGenerator.GenerateListSchemaXml(_spSourceFileInfo.SPSourceFileDirectoryInfo, listName, _spSourceFileInfo.DesignSiteUrl, listTemplateType);
                        
                        if (!_spSourceFileInfo.OverrideFeatureDirectoryCheck)
                            FeatureManager.AddElementFileToFeature(schemaFile);

                        ListTemplateDependentFilesGenerator.GenerateDependentFiles(_spSourceFileInfo.SPSourceFileDirectoryInfo, listName, _spSourceFileInfo.DesignSiteUrl, _spSourceFileInfo.OverrideFeatureDirectoryCheck);
                        listTemplateManifestGenerator.CreateListTemplateNode(listName, _spSourceFileInfo.DesignSiteUrl, listTemplateType);
                        listTemplateManifestGenerator.CreateListInstanceNode(listName, _spSourceFileInfo.DesignSiteUrl, listTemplateType, _spSourceFileInfo.FeatureId);

                        _listTemplateTypeCounter++;
                    }

                    FileInfo elementManifestFile = listTemplateManifestGenerator.SaveListManifest(_spSourceFileInfo.SPSourceFileDirectoryInfo, _spSourceFileInfo.SPSourceFileName);

                    if (!_spSourceFileInfo.OverrideFeatureDirectoryCheck) 
                        FeatureManager.AddElementManifestToFeature(elementManifestFile);
                }
            }
            catch (Exception ex)
            {
                Utility.WriteError(ex.ToString());
                throw;
            }
		}
    }
}
